local function Load_UserGroups()
	local SQL = "SELECT * FROM KKB_UserGroups"
	local function Callback( result )
		local UserGroups = {}
		for _,v in pairs(result) do
			local Group = v.UserGroup
			local Spawn = util.JSONToTable(v.SpawnSettings)
			local Physgun = util.JSONToTable(v.PhysgunSettings)
			local Toolgun = util.JSONToTable(v.ToolgunSettings)
			local Gravcannon = util.JSONToTable(v.GravcannonSettings)
			local Damage = util.JSONToTable(v.DamageSettings)
			local Use = util.JSONToTable(v.UseSettings)
			local Other = util.JSONToTable(v.OtherSettings)
			local Donate = util.JSONToTable(v.DonateSettings)
			UserGroups[Group] = {}
			UserGroups[Group].Spawn = Spawn
			UserGroups[Group].Physgun = Physgun
			UserGroups[Group].Toolgun = Toolgun
			UserGroups[Group].Gravcannon = Gravcannon
			UserGroups[Group].Damage = Damage
			UserGroups[Group].Use = Use
			UserGroups[Group].Other = Other
			UserGroups[Group].Donate = Donate
		end
		table.Empty( KKB.UserGroups )
		KKB.UserGroups = UserGroups
		print( "[KKB][MySQL]: User Groups Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_CustomUsers()
	local SQL = "SELECT * FROM KKB_CustomUsers"
	local function Callback( result )
		local CustomUsers = {}
		for _,v in pairs(result) do
			local UniqueID = v.UniqueID
			local Nick = v.Nick
			local Group = v.UserGroup
			CustomUsers[UniqueID] = {}
			CustomUsers[UniqueID].Nick = Nick
			CustomUsers[UniqueID].Group = Group
		end
		table.Empty( KKB.CustomUsers )
		KKB.CustomUsers = CustomUsers
		print( "[KKB][MySQL]: Custom Users Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_Settings()
	local SQL = "SELECT * FROM KKB_Settings"
	local function Callback( result )
		if ( result[1] != nil ) then
			local Settings = {}
			for _,v in pairs(result) do
				Settings = util.JSONToTable( v.SettingTypes )
			end
			table.Empty( KKB.Settings )
			KKB.Settings = Settings
			print( "[KKB][MySQL]: Settings Loaded OK!" )
		end
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_Damage()
	local SQL = "SELECT * FROM KKB_Damage"
	local function Callback( result )
		if ( result[1] != nil ) then
			local Damage = {}
			for _,v in pairs(result) do
				Damage = util.JSONToTable( v.DamageTypes )
			end
			table.Empty( KKB.Damage )
			KKB.Damage = Damage
			print( "[KKB][MySQL]: Damage Loaded OK!" )
		end
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_BlockedModels()
	local SQL = "SELECT * FROM KKB_Models"
	local function Callback( result )
		local Models = {}
		for _,v in pairs(result) do
			local Model = v.Model
			table.insert(Models, Model)
		end
		table.Empty( KKB.BlockedModels )
		KKB.BlockedModels = Models
		print( "[KKB][MySQL]: Models Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_EntityRestrictions()
	local SQL = "SELECT * FROM KKB_Entitys"
	local function Callback( result )
		local Entitys = {}
		for _,v in pairs(result) do
			local Entity = v.EntityName
			local UserGroups = util.JSONToTable( v.UserGroups )
			local Options = util.JSONToTable( v.Options )
			Entitys[Entity] = {}
			Entitys[Entity].Groups = UserGroups
			Entitys[Entity].Options = Options
		end
		table.Empty( KKB.EntityRestrictions )
		KKB.EntityRestrictions = Entitys
		print( "[KKB][MySQL]: Entitys Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_NPCRestrictions()
	local SQL = "SELECT * FROM KKB_NPCs"
	local function Callback( result )
		local NPCs = {}
		for _,v in pairs(result) do
			local NPC = v.NPCName
			local UserGroups = util.JSONToTable( v.UserGroups )
			local Options = util.JSONToTable( v.Options )
			NPCs[NPC] = {}
			NPCs[NPC].Groups = UserGroups
			NPCs[NPC].Options = Options
		end
		table.Empty( KKB.NPCRestrictions )
		KKB.NPCRestrictions = NPCs
		print( "[KKB][MySQL]: NPCs Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_VehicleRestrictions()
	local SQL = "SELECT * FROM KKB_Vehicles"
	local function Callback( result )
		local Vehicles = {}
		for _,v in pairs(result) do
			local Vehicle = v.VehicleName
			local UserGroups = util.JSONToTable( v.UserGroups )
			local Options = util.JSONToTable( v.Options )
			Vehicles[Vehicle] = {}
			Vehicles[Vehicle].Groups = UserGroups
			Vehicles[Vehicle].Options = Options
		end
		table.Empty( KKB.VehicleRestrictions )
		KKB.VehicleRestrictions = Vehicles
		print( "[KKB][MySQL]: Vehicles Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_WeaponRestrictions()
	local SQL = "SELECT * FROM KKB_Weapons"
	local function Callback( result )
		local Weapons = {}
		for _,v in pairs(result) do
			local Weapon = v.WeaponName
			local UserGroups = util.JSONToTable( v.UserGroups )
			local Options = util.JSONToTable( v.Options )
			Weapons[Weapon] = {}
			Weapons[Weapon].Groups = UserGroups
			Weapons[Weapon].Options = Options
		end
		table.Empty( KKB.WeaponRestrictions )
		KKB.WeaponRestrictions = Weapons
		print( "[KKB][MySQL]: Weapon Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

local function Load_ToolgunRestrictions()
	local SQL = "SELECT * FROM KKB_Tools"
	local function Callback( result )
		local Tools = {}
		for _,v in pairs(result) do
			local Tool = v.ToolName
			local UserGroups = util.JSONToTable( v.UserGroups )
			local Options = util.JSONToTable( v.Options )
			Tools[Tool] = {}
			Tools[Tool].Groups = UserGroups
			Tools[Tool].Options = Options
		end
		table.Empty( KKB.ToolgunRestrictions )
		KKB.ToolgunRestrictions = Tools
		print( "[KKB][MySQL]: Tools Loaded OK!" )
	end
	KKB.DBQuery( SQL, Callback )
end

function KKB.DBLoad()
	Load_UserGroups()
	Load_CustomUsers()
	Load_Settings()
	Load_Damage()
	Load_BlockedModels()
	Load_EntityRestrictions()
	Load_NPCRestrictions()
	Load_VehicleRestrictions()
	Load_WeaponRestrictions()
	Load_ToolgunRestrictions()
end